<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="线路标识" prop="lineId">
        <el-select v-model="queryParams.lineId" placeholder="请选择线路标识" clearable>
          <el-option
              v-for="dict in userLineList"
              :key="dict.lineId"
              :label="dict.lineName"
              :value="dict.lineId"
          />
        </el-select>
      </el-form-item>
<!--      <el-form-item label="光纤通道" prop="fiberNum">
        <el-select v-model="queryParams.fiberNum" placeholder="请选择光纤通道" clearable>
          <el-option
              v-for="dict in fiber_info"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
          />
        </el-select>
      </el-form-item>-->
      <el-form-item label="检测时间" style="width: 308px">
        <el-date-picker
            v-model="daterangeCreationTs"
            value-format="YYYY-MM-DD"
            type="daterange"
            range-separator="-"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="状态" prop="status" style="width: 200px">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable>
          <el-option
              v-for="dict in warn_his_status"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
<!--      <el-col :span="1.5">
        <el-button
            type="primary"
            plain
            icon="Plus"
            @click="handleAdd"
            v-hasPermi="['warn:warnHis:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="success"
            plain
            icon="Edit"
            :disabled="single"
            @click="handleUpdate"
            v-hasPermi="['warn:warnHis:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="danger"
            plain
            icon="Delete"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['warn:warnHis:remove']"
        >删除</el-button>
      </el-col>-->
      <el-col :span="1.5">
        <el-button
            type="danger"
            plain
            icon="Delete"
            :disabled="multiple"
            @click="handleIgnore"
            v-hasPermi="['warn:warnHis:remove']"
        >忽略</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="warning"
            plain
            icon="Download"
            @click="handleExport"
            v-hasPermi="['warn:warnHis:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="warnHisList" @selection-change="handleSelectionChange"
              stripe>
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="流水号" align="center" prop="id" v-if="false"/>
      <el-table-column label="线路标识" align="center" prop="lineId" width="110" >
        <template #default="scope">
          <dict-tag :options="line_info" :value="scope.row.lineId"/>
        </template>
      </el-table-column>
      <el-table-column label="光纤通道编号" align="center" prop="fiberNum" width="130">
        <template #default="scope">
          <dict-tag :options="fiber_info" :value="scope.row.fiberNum"/>
        </template>
      </el-table-column>
      <el-table-column label="检测时间" align="center" prop="creationTs" width="160">
<!--        <template #default="scope">
          <span>{{ parseTime(scope.row.creationTs, '{y}-{m}-{d}') }}</span>
        </template>-->
      </el-table-column>
      <el-table-column label="预警分段区域的编号" align="center" prop="warnSectionNo" width="160"/>
      <el-table-column label="预警编号" align="center" prop="warnNo" width="160"/>
      <el-table-column label="起点距离(m)" align="center" prop="startDistance" width="100px"/>
      <el-table-column label="终点距离(m)" align="center" prop="endDistance" width="100px"/>
      <el-table-column label="峰值距离(m)" align="center" prop="peakDistance" width="100px"/>
      <el-table-column label="峰值数值" align="center" prop="peakValue" width="100px"/>
      <el-table-column label="预警级别" align="center" prop="warnLevel" width="90px"/>
      <el-table-column label="状态" align="center" prop="status" width="100px">
        <template #default="scope">
          <dict-tag :options="warn_his_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="150">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['warn:warnHis:edit']">查看</el-button>
          <el-button link type="primary" icon="Delete" @click="handleIgnore(scope.row)" v-hasPermi="['warn:warnHis:remove']">忽略</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
        v-show="total>0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
    />

    <!-- 添加或修改预警历史记录对话框 -->
    <el-dialog :title="title" v-model="open" width="600px" append-to-body>
      <el-form ref="warnHisRef" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="线路标识" prop="lineId">
          <el-select v-model="form.lineId" placeholder="请选择线路标识">
            <el-option
                v-for="dict in line_info"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="光纤通道" prop="fiberNum">
          <el-select v-model="form.fiberNum" placeholder="请选择光纤通道">
            <el-option
                v-for="dict in fiber_info"
                :key="dict.value"
                :label="dict.label"
                :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="检测时间" prop="creationTs">
<!--          <el-date-picker clearable-->
<!--                          v-model="form.creationTs"-->
<!--                          type="date"-->
<!--                          value-format="YYYY-MM-DD HH:mm:ss"-->
<!--                          placeholder="请选择检测时间">-->
<!--          </el-date-picker>-->
          <el-input v-model="form.creationTs" placeholder="请选择检测时间" />
        </el-form-item>
        <el-form-item label="预警分段区域的编号" prop="warnSectionNo">
          <el-input v-model="form.warnSectionNo" placeholder="请输入预警分段区域的编号" />
        </el-form-item>
        <el-form-item label="预警编号" prop="warnNo">
          <el-input v-model="form.warnNo" placeholder="请输入预警编号" />
        </el-form-item>
        <el-form-item label="起点距离(m)" prop="startDistance">
          <el-input v-model="form.startDistance" placeholder="请输入起点距离(m)" />
        </el-form-item>
        <el-form-item label="终点距离(m)" prop="endDistance">
          <el-input v-model="form.endDistance" placeholder="请输入终点距离(m)" />
        </el-form-item>
        <el-form-item label="峰值距离(m)" prop="peakDistance">
          <el-input v-model="form.peakDistance" placeholder="请输入峰值距离(m)" />
        </el-form-item>
        <el-form-item label="峰值数值" prop="peakValue">
          <el-input v-model="form.peakValue" placeholder="请输入峰值数值" />
        </el-form-item>
        <el-form-item label="预警级别" prop="warnLevel">
          <el-input v-model="form.warnLevel" placeholder="请输入预警级别" />
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio
                v-for="dict in warn_his_status"
                :key="dict.value"
                :label="dict.value"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="处理人" prop="warnLevel">
          <el-input v-model="form.updateBy" placeholder="请输入处理人" />
        </el-form-item>
        <el-form-item label="处理时间" prop="warnLevel">
          <el-input v-model="form.updateTime" placeholder="请输入处理时间" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
<!--          <el-button type="primary" @click="submitForm">确 定</el-button>-->
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="WarnHis">
import { listWarnHis, getWarnHis, delWarnHis, addWarnHis, updateWarnHis, ignoreWarnHis } from "@/api/warn/warnHis";
import {lineList} from "@/api/meas/lineUsers"
import {onMounted} from "vue";

const { proxy } = getCurrentInstance();
const { warn_his_status, line_info, fiber_info } = proxy.useDict('warn_his_status', 'line_info', 'fiber_info');

const warnHisList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const daterangeCreationTs = ref([]);

const userLineList = ref([])

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    lineId: null,
    fiberNum: null,
    creationTs: null,
    status: null,
  },
  rules: {
    lineId: [
      { required: true, message: "线路标识不能为空", trigger: "change" }
    ],
    fiberNum: [
      { required: true, message: "光纤通道编号不能为空", trigger: "change" }
    ],
    creationTs: [
      { required: true, message: "检测时间不能为空", trigger: "blur" }
    ],
    warnSectionNo: [
      { required: true, message: "预警分段区域的编号不能为空", trigger: "blur" }
    ],
    warnNo: [
      { required: true, message: "预警编号不能为空", trigger: "blur" }
    ],
    startDistance: [
      { required: true, message: "起点距离(m)不能为空", trigger: "blur" }
    ],
    endDistance: [
      { required: true, message: "终点距离(m)不能为空", trigger: "blur" }
    ],
    peakDistance: [
      { required: true, message: "峰值距离(m)不能为空", trigger: "blur" }
    ],
    peakValue: [
      { required: true, message: "峰值数值不能为空", trigger: "blur" }
    ],
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询预警历史记录列表 */
function getList() {
  loading.value = true;
  queryParams.value.params = {};
  if (null != daterangeCreationTs && '' != daterangeCreationTs) {
    queryParams.value.params["beginCreationTs"] = daterangeCreationTs.value[0];
    queryParams.value.params["endCreationTs"] = daterangeCreationTs.value[1];
  }
  listWarnHis(queryParams.value).then(response => {
    warnHisList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

onMounted(()=>{
  console.log(line_info)

  lineList().then(resp => {
    userLineList.value = resp.data
  })

})

// 表单重置
function reset() {
  form.value = {
    id: null,
    lineId: null,
    fiberNum: null,
    creationTs: null,
    warnSectionNo: null,
    warnNo: null,
    startDistance: null,
    endDistance: null,
    peakDistance: null,
    peakValue: null,
    warnLevel: null,
    status: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null
  };
  proxy.resetForm("warnHisRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  daterangeCreationTs.value = [];
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加预警历史记录";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value
  getWarnHis(_id).then(response => {
    form.value = response.data;
    open.value = true;
    // title.value = "修改预警历史记录";
    title.value = "查看预警历史记录";
  });
}

function handleIgnore(row) {
  const _ids = row.id || ids.value;
  console.log(_ids)
  let count = 0
  let pIds = []
  if (!_ids.length) {
    count = 1
    pIds.push(_ids)
  } else {
    count = _ids.length
    pIds = _ids
  }
  proxy.$modal.confirm('是否确认忽略预警历史记录' + count + '条数据项？').then(function() {
    let data = {"ids": pIds}
    return ignoreWarnHis(data);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("操作成功");
  }).catch(() => {});
}
/** 提交按钮 */
function submitForm() {
  proxy.$refs["warnHisRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateWarnHis(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addWarnHis(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除预警历史记录编号为"' + _ids + '"的数据项？').then(function() {
    return delWarnHis(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('warn/warnHis/export', {
    ...queryParams.value
  }, `warnHis_${new Date().getTime()}.xlsx`)
}

getList();
</script>
